/*
 * @Author: Xujianchen
 * @Date: 2024-08-01 11:45:34
 * @LastEditors: Xujianchen
 * @LastEditTime: 2024-08-01 11:51:05
 * @Description: 防抖指令
 */
import { isFunc, isObject } from '@/utils/type'
import { debounce } from '@/utils/utils'

export default {
  mounted(el, binding) {
    if (!isObject(binding.value)) {
      throw 'debounce directive value must be an object'
    }
    if (!isFunc(binding.value.fn)) {
      throw 'debounce directive value.fn must be a function'
    }

    const { fn, delay = 500, immediate = false } = binding.value
    el._handleClick_ = debounce(fn, delay, immediate)
    el.addEventListener('click', el._handleClick_)
  },
  beforeUnmount(el) {
    el.removeEventListener('click', el._handleClick_)
  },
}
